package com.example.tool.window;

import com.example.tool.module.ModuleInformation;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class RefreshAction extends DumbAwareAction {
  private final Map<Integer, Runnable> runnableMap = new HashMap<>();

  public RefreshAction() {
    super(ModuleInformation.REFRESH, "", AllIcons.Actions.Refresh);
  }

  /**
   * 初始化刷新 用于判断不同步骤进度下的刷新执行动作
   * @param index    步骤进度
   * @param runnable 具体的执行操作
   */
  public void initRefresh(int index, Runnable runnable) {
    runnableMap.put(index, runnable);
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    AtomicInteger step = HToolWindowFactory.windowEntityMap.get(project).getStep();
    int i = step.get();
    Runnable runnable = runnableMap.get(i);
    if (runnable != null) {
      runnable.run();
    }
  }
}
